//
//  ZLQWebViewController.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/12.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class ZLQWebViewController: UIViewController,UIWebViewDelegate {

    
    let urlString1 = "http://q.chanyouji.com/api/v1/inspiration_activities/"
    let urlString2 = ".json"
    
    var aaInt = Int()
    
    var webview = UIWebView()
    
    var request = NSURLRequest()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        dataAnalysis()

        
        // Do any additional setup after loading the view.
    }
    
    func dataAnalysis() {
    let string:String = urlString1 + String(self.aaInt) + urlString2
        let url = NSURL(string: string)
        print(url!)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!) { (data:NSData?, re:NSURLResponse?, error:NSError?) -> Void in
            
            do {
                let dic:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let dict = dic["data"] as! NSDictionary
                if dict["user_activity"] != nil {
                let dicti = dict["user_activity"] as! NSDictionary
                    let dictio = dicti["poi"] as! NSDictionary
                    let model = ZLQWebViewModel()
                    model.setValuesForKeysWithDictionary(dictio as! [String : AnyObject])
                    self.request = NSURLRequest(URL: NSURL(string: model.h5_url!)!)
                } else {
                   let dictio1 = dict["pois"] as! NSArray
                    for diction2 in dictio1 {
                        let model = ZLQWebViewModel()
                        model.setValuesForKeysWithDictionary(diction2 as! [String : AnyObject])
                    self.request = NSURLRequest(URL: NSURL(string: model.h5_url!)!)
                    }
                    
                
                }
                
                
         
                
    
                
            
            
            } catch {
            }
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                
                self.webview.frame = CGRectMake(0, 59                                              ,self.view.frame.size.width, self.view.frame.size.height)
                self.view.addSubview(self.webview)
          
                self.webview.delegate = self
                    self.webview.loadRequest(self.request)
            })
            
        }
        task.resume()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
